<?php
class shop{
	var $db;
	var $webUrl;
	var $tpl;
	
	function shop()
	{
		global $db,$tpl;
		$this -> db     = & $db;
		$this -> tpl    = & $tpl;
		$this -> view_user_times();
	}
	############################
	function site_spread($uid='')
	{
		global $config;
		include("config/point_config.php");
		$cip=getip();
		$nt=time();
		$gt=$nt-3600;
		$sql="select * from ".SPREAD." where userid='$uid' and fromip='$cip' and ctime>$gt";
		$this->db->query($sql);
		$re=$this->db->fetchRow();
		if(!empty($re['userid']))
		{
			$sql="update ".SPREAD." set access_num=access_num+1,ctime='$nt' where userid='$uid' and fromip='$cip' and ctime>$gt";
			$this->db->query($sql);
		}
		else
		{
			$sql="insert into ".SPREAD." (`userid`,`fromip`,`ctime`) values ('$uid','$cip','$nt')";
			$this->db->query($sql);
			$sql="update ".ALLUSER." set point=point+$point_config[promote] where userid='$uid'";
			$this->db->query($sql);
		}
		return true;
	}
	function get_custom_cat_list($type="")
	{
		global $config;
		if($type=='1')
		{
			$str=" and pid='0'";	
		}
		$sql="select * from ".CUSTOM_CAT." where userid='$_GET[uid]' and type='$type' $str order by nums asc";
		$this->db->query($sql);
		$re=$this->db->getRows();
		for($i=0;$i<count($re);$i++)
		{
			$sql="select count(*) as custom_cat_count from ".CUSTOM_CAT_REL." where custom_cat_id = ".$re[$i]['id'];	
			$this->db->query($sql);
			$custom_rel_list=$this->db->fetchRow();
			$re[$i]["count"] = $custom_rel_list['custom_cat_count'];
		}
		return $re;
	}
	function user_detail($uid)
	{	
		global $config,$action;
		if(!is_numeric($uid))
			$sql="select userid,statu,name,position,qq,email,mobile,province,city,skype,logo as plogo,regtime
			from ".ALLUSER." WHERE user='$uid'";
		else
			$sql="select userid,statu,name,position,qq,email,mobile,province,city,skype,logo as plogo,regtime
			from ".ALLUSER." WHERE userid='$uid'";
		$this ->db ->query($sql);
		$com=$this ->db->fetchRow();
		if($com)
		{	
			$_GET["uid"]=$uid;
			//----------------------country-------------
			if($config['language']=='cn')
				$sqlo="select cname as countryname from ".COUNTRY." where id='".$com['country']."'";
			else
				$sqlo="select ename as countryname from ".COUNTRY." where id='".$com['country']."'";
			$this ->db ->query($sqlo);
            $com["country"]=$this->db->fetchField('countryname');
			//----------------------COMPANYCAT------------------
			if (!empty($com["catid"]))
			{
				$strcat=explode(",",$com["catid"]);
				$st=implode("','",$strcat);
				$sqlo="select cat  from ".COMPANYCAT." where catid in ('".$st."')";
				$this ->db ->query($sqlo);
				$catn=$this ->db->getRows();
				$com["catname"]='';
				foreach($catn as $vc)
				{
					$com["catname"].=$vc['cat'].'&nbsp;|&nbsp;';
				}
			}
			//----------------------------------------------------
			$sql="select * from ".USER." where userid='$com[userid]'";
			$this->db->query($sql);
			$re=$this->db->fetchRow();
			if(is_array($re))
				$com=array_merge($com,$re);
			$com["time_long"]=date("Y")-substr($com["regtime"],0,4)+1;
			//----------------------------------------------------
			$ifpay=empty($com['ifpay'])?1:$com['ifpay'];
			include($config['webroot']."/config/usergroup.php");
			$medallog=empty($group[$ifpay]['logo'])?$config['weburl']."/image/default/nopic.gif":$group[$ifpay]['logo'];
			$com['medal']="<img src=".$medallog." />";
			$com['rank']=$group[$ifpay]['name'];
			$com['jsbook']=$group[$ifpay]['modeu']['jsbook'];
			$com['open_info_type']=$group[$ifpay]['modeu']['open_info_type'];
			unset($group);
			//----------------------------------------------------
			$this ->tpl->assign("com",$com);
			return $com;
		}
		else
			msg($config['weburl'].'/404.php');
	}
	function get_user_detail($uid,$type=1)
	{
		$sql="select intro,logo as img from ".UDETAIL." where userid='$uid' and type=$type";
		$this->db->query($sql);
		return $this->db->fetchRow();
	}
	function get_user_link()
	{
		if(isset($_GET['uid']))
		{
			$sql="select * from ".ULINK." WHERE userid='$_GET[uid]'";
			$this->db->query($sql);
			return $this->db->getRows();
		}
	}
	function view_user_times()
	{
		$sql="update ".USER." SET view_times=view_times+1 WHERE userid='$_GET[uid]'";
		$this ->db ->query($sql);
	}
	//  lolololo
	function get_total_rows_by_userid($table="")
	{
		$sql="select count(*) as totalrows from ".$table." where userid='$_GET[uid]'";
		$this->db->query($sql);
		$re=$this->db->fetchRow();
		return $re['totalrows'];
	}

	function get_business_validate()
	{
		$sql="select statu from ".BUSINESS." where userid='$_GET[uid]'";
		$this->db->query($sql);
		$re=$this->db->fetchRow();
		return isset($re['statu']) ? $re['statu'] : 0;
	}
	function business_info_detail()
	{
		$sql="select * from ".BUSINESS." where userid='$_GET[uid]'";
		$this->db->query($sql);
		$ks=$this->db->fetchRow();
		if($ks['com_reg_id_protect']==1)
		{
			$ks['com_reg_id'] = substr($ks['com_reg_id'],0,strlen($ks['com_reg_id'])-4);
			$ks['com_reg_id'] .= '****';
		}
		return $ks;
	}
}
?>
